<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Template extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Templatemodel','m_template',true);
    }

    public function index(){
        $data = parent::index();

        $data['attr'] = array();

        $this->load->view('admin/template',$data);
    }

    public function getTemplateList(){
        $assist['where'] = getFilter($this->input->post(),Templatemodel::$searchField);
        $assist['like'] = getFilter($this->input->post(),Templatemodel::$likeField);
        $assist['sort'] = getSort($this->input->post(),Templatemodel::$sortField);
        $assist['pagination'] = getPagination($this->input->post());

        list($list,$count) = $this->m_template->getTemplateListSection($assist);

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function createTemplate(){
        $this->validate->setRule('template_name','string');
        $this->validate->setRule('template_money','float');
        $this->validate->setRule('template_money','notnull',false);
        $this->validate->setRule('template_money','optional');
        $this->validate->setRule('template_support','string');
        $this->validate->setRule('template_wish','string');
        if(!$this->validate->run($this->input->post())){
          $this->response->setDetail($this->validate->getMessage());
          $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        if($info['template_money'] < 1) {
          $this->response->setDetail('金额不能小于1块');
          $this->response->responseExit();
        }

        $info['user_id'] = $this->m_app->getCurrentUserId();
        $info['template_status'] = STATUS_PENDING;
        $info['template_time_create'] = NOW;
        $this->m_template->createTemplate($info);

        $this->response->setSuccess();
        $this->response->responseExit();
      }

    public function editTemplate(){
  		$this->validate->setRule('template_id','integer');
      $this->validate->setRule('template_name','string');
      $this->validate->setRule('template_money','float');
      $this->validate->setRule('template_money','notnull',false);
      $this->validate->setRule('template_money','optional');
      $this->validate->setRule('template_support','string');
      $this->validate->setRule('template_wish','string');
  		if(!$this->validate->run($this->input->post())){
  			$this->response->setDetail($this->validate->getMessage());
  			$this->response->responseExit();
  		}
  		$info = $this->validate->getResult();

      if($info['template_money'] < 1) {
        $this->response->setDetail('金额不能小于1块');
        $this->response->responseExit();
      }

  		$info['user_id'] = $this->m_app->getCurrentUserId();
  		$this->m_template->editTemplate($info);

  		$this->response->setSuccess();
  		$this->response->responseExit();
  	}

    public function deleteTemplate(){
  		$this->validate->setRule('template_id','integer');
      if(!$this->validate->run($this->input->post())){
        $this->response->setDetail($this->validate->getMessage());
        $this->response->responseExit();
      }
      $info = $this->validate->getResult();
      $info['user_id'] = $this->m_app->getCurrentUserId();
      $info['template_status'] = STATUS_DELETE;

      $this->m_template->editTemplate($info);


      $this->response->setSuccess();
      $this->response->responseExit();
    }


}
/* End of file api_log.php */
/* Location: ./application/controllers/api_log.php */
